﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class ClimbEntity : MonoBehaviour
{
    public Transform top;

}

public class BlockEntity : ClimbEntity
{
    public int fitItem = 1;

    float destroyDis = 1;

    private bool m_Die;
    public bool dead => m_Die;
    
    private Rigidbody[] m_Rigids;
    
    ZTransformAnim zTransformAnim;
    public ZTransformAnim transformAnim
    {
        get
        {
            if (zTransformAnim == null) zTransformAnim = this.gameObject.AddComponent<ZTransformAnim>();
            return zTransformAnim;
        }
    }

    public ColliderTrigger playerTrigger;
    
    // Start is called before the first frame update
    void Awake()
    {
        m_Rigids = GetComponentsInChildren<Rigidbody>();
        playerTrigger.onTriggerEnter = DoOnPlayerCollider;
        playerTrigger.transform.SetParent(transform.parent);
    }

    private void DoOnPlayerCollider(ColliderTrigger arg1, Collider collider)
    {
       // if (m_Die) 
         //   return;
        
        if (collider.gameObject.layer == MBattleController.layerPlayer)
        {
            Player player = collider.GetComponent<Player>();
            if (player)
            {
                //player.OnTriggerWall(this);
            }
        }
    }

    private void OnTriggerEnter(Collider collider)
    {
        if (m_Die) 
            return;
        
        if (collider.gameObject.layer == MBattleController.layerPlayer)
        {
            Player player = collider.GetComponent<Player>();
            if (player)
            {
                //player.OnTriggerClimbWall(this);
            }
        }
    }
        


    
    public void OnDrillDestroy(Player player,float yDis = .4f)
    {
        m_Die = true;
        
        player.score += player.mBattleController.MBattleDataConfig.killWallScore;
        
        var pos = player.capsuleCollider.bounds.center;
        foreach (var rigidbody in m_Rigids)
        {
            if (!rigidbody) continue;
            
            Vector3 vec = transform.InverseTransformPoint(rigidbody.transform.position) -  transform.InverseTransformPoint(pos);
            vec.z = 0;
            if ( Mathf.Abs(vec.x) < this.destroyDis)// && rigidbody.position.y < player.capsuleCollider.bounds.max.y + yDis)
            {
                if (!rigidbody.isKinematic) 
                    continue;
                
                rigidbody.isKinematic = false;
                rigidbody.AddForce(Random.Range(4,15) *(rigidbody.transform.position - pos).normalized, ForceMode.Impulse);
                rigidbody.AddTorque(Random.Range(1,30) *Vector3.forward,ForceMode.Impulse);
                rigidbody.gameObject.layer = MBattleController.layerIgnoreRaycast;
                rigidbody.transform.SetParent(player.mBattleController.dynamicWidgetsNode);
                transformAnim.ScaleTo(rigidbody.transform, new Vector3(0.001f, 0.001f, 0.001f), 1.5f, null);
                Destroy(rigidbody.gameObject,3);
            }
            else
            {
                var col = rigidbody.GetComponent<Collider>();
                if(col)
                    col.isTrigger = true;
            }
        }

        PlayHitFx(transform.position + new Vector3(0,1.5f,0));

        AudioController.instance.PlayBattleSound("sound_hit_wall", transform.position, .5f);
    }
    
    void PlayHitFx(Vector3 pos)
    {
        var pref = GameResourceManager.Load<GameObject>(GameResourceManager.hitFxPath);
        var fx = Instantiate(pref, null);
        fx.transform.position = pos;
        Destroy(fx.gameObject, 3);
    }
    
    public void OnDrillDestroy(Vector3 pos)
    {
        foreach (var rigidbody in m_Rigids)
        {
            Vector3 vec = transform.InverseTransformPoint(rigidbody.transform.position) -  transform.InverseTransformPoint(pos);
            vec.z = 0;
            if ( Mathf.Abs(vec.x) < 2)
            {
                if (!rigidbody.isKinematic) 
                    continue;
                
                rigidbody.isKinematic = false;
                rigidbody.AddForce(Random.Range(10,30) *(rigidbody.transform.position - pos).normalized, ForceMode.Impulse);
                rigidbody.gameObject.layer = MBattleController.layerIgnoreRaycast;
                rigidbody.transform.SetParent(transform.parent);
                transformAnim.ScaleTo(rigidbody.transform, new Vector3(0.001f, 0.001f, 0.001f), 1.5f, null);
                Destroy(rigidbody.gameObject,3);
            }
        }

        AudioController.instance.PlayBattleSound("sound_hit_wall", transform.position, .5f);
    }
    


    // Update is called once per frame
    void Update()
    {
        
    }
}
